package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.ErrorCodes;
import unipi.database.Tasks;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Validator;

public class EditTask extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public EditTask()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, String taskID, String message)
	{
		String content = "<h1>Επεξεργασία Εργασίας</h1>";
			
		Map<String, String> taskData = Tasks.get(taskID, false);
		
		if(taskData == null)
		{
			content += "Η εργασία αυτή δεν υπάρχει.";
		}		
		else
		{
			if(!uid.equals(taskData.get("assignerID")))
			{
				content += "Δεν έχετε δικαίωμα να επεξεργαστείτε αυτή την εργασία.";
			}
			else
			{			
				content += 	HTMLPages.getSubmitForm("EditTask",
						new String[]{
							"",
							"*Τίτλος",
							"*Περιγραφή",
							"Ομάδα",
							"*Απαιτούμενες Ώρες",
							"*Ημερομηνία Έναρξης",
							"*Προθεσμία",
							"Διαγραφή;"},
						new String[]{
							"<input type=\"hidden\" name=\"id\" value=\"" + taskID + "\" />",
							"<input class=\"mediumInput\" name=\"title\" type=\"text\" value=\"" + taskData.get("title") +"\" />",
							"<textarea  class=\"txtarea\" name=\"desc\" rows=\"6\" cols=\"40\" >" +
							taskData.get("description") +
							"</textarea>",
							"<input class=\"mediumInput\" name=\"group\" type=\"text\" value=\"" + taskData.get("group") +"\" />",
							"<input class=\"mediumInput\" name=\"hours\" type=\"text\" value=\"" + taskData.get("hoursRequired") +"\" />",
							"<input class=\"mediumInput\" name=\"startdate\" type=\"text\" value=\"" + taskData.get("startDate") +"\" />",
							"<input class=\"mediumInput\" name=\"duedate\" type=\"text\" value=\"" + taskData.get("dueDate") + "\" />",
							"<input type=\"checkbox\" name=\"delete\" value=\"del\" />"
						},"Ενημέρωση") + message;
			}
			
		}
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];	
			
			request.setCharacterEncoding("utf-8");
			String taskID = request.getParameter("id");
			String title = request.getParameter("title");
			String desc = request.getParameter("desc");
			String group = request.getParameter("group");
			String hours = request.getParameter("hours");
			String startDate = request.getParameter("startdate");
			String dueDate = request.getParameter("duedate");
			boolean delete = request.getParameter("delete") != null;
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
							
			if(delete)
			{
				ErrorCodes e = Tasks.delete(taskID);
				if(e == ErrorCodes.OK)
				{
					response.sendRedirect("ViewTasks?view=mytasks");
				}
				else
				{
					out.println(getPage(uid, taskID,  "Η εργασία δεν μπόρεσε να διαγραφεί."));
				}
			}
			else
			{			
				title = Validator.escape(title);
				desc = Validator.escape(desc);
				if(group.equals(""))
					group = null;
				else
					group = Validator.escape(group);
				
				if(taskID == null)
					response.sendRedirect("ViewTasks?view=mytasks");
				else if(!Validator.isNumber(taskID))
					response.sendRedirect("ViewTasks?view=mytasks");
				else
				{	
					if(title != null && desc != null && hours != null && startDate != null && dueDate != null)
					{			
						String message = "Η εργασία ενημερώθηκε με επιτυχία.";
		
						if(title.equals("") || desc.equals("") || hours.equals("") || startDate.equals("") || dueDate.equals(""))
						{
							message = "Πρέπει να συμπληρώσετε όλα τα πεδία εισόδου με *.";
						}
						else if(!Validator.isDate(startDate) || !Validator.isDate(dueDate))
						{
							message = "Οι ημερομηνίες πρέπει να ειναι της μορφής ΥΥΥΥ-ΜΜ-DD.";
						}
						else if(!Validator.isNumber(hours))
						{
							message = "Το πεδίο Απαιτούμενες Ώρες πρέπει να είναι αριθμός.";
						}
						else
						{				
							ErrorCodes edit = Tasks.edit(taskID, title, desc, group, hours, startDate, dueDate);	
											
							if(edit != ErrorCodes.OK)
							{
								message = "Παρουσιάστηκε πρόβλημα κατα την ενημέρωση της εργασίας.";
							}
						}
						
						out.println(getPage(uid, taskID, message));
					}
					else
					{
						out.println(getPage(uid, taskID,  "Δεν δώθηκαν όλα τα στοιχεία."));
					}
					
				}
			}
			
			out.close();
		}

	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];			
	
			request.setCharacterEncoding("utf-8");
			String taskID = request.getParameter("id");
			
			if(taskID == null)
				response.sendRedirect("ViewTasks?view=mytasks");
			else if(!Validator.isNumber(taskID))
				response.sendRedirect("ViewTasks?view=mytasks");
			else
			{	
				response.setContentType("text/html; charset=UTF-8");	
				PrintWriter out = response.getWriter();
				
				out.println(getPage(uid, taskID, ""));
				
				out.close();	
			}

		}
	}
	
}
